<docs>
  ## 编辑个人基本信息
</docs>
<template>
  <div class="wrap" ref="wrap" :style="{minHeight: formHeight}">
    <my-bread title="基本信息"></my-bread>
    <co-form :model="data" :rules="rules" ref="form">
      <co-input label="身份证号码"
        v-model="data.sfzhm" placeholder="请输入身份证号码" :disabled="disTrue"></co-input>
      <co-input label="出生日期"
        v-model="data.csrq_xs" placeholder="请输入出生日期" :disabled="disTrue"></co-input>
      <co-input label="性别"
        v-model="data.xb_xs" placeholder="请输入性别" :disabled="disTrue"></co-input>
      <co-input prop="xm" label="姓名"
        v-model="data.xm" placeholder="请输入姓名"></co-input>
      <co-select prop="whcd" label="文化程度" v-model="data.whcd"
        :list="codeTable.zyjsww_d_whcd"></co-select>
      <co-level-picker
          prop="sxzy"
          label="所学专业"
          ref="zy"
          v-model="data.sxzy"
          v-show="sxzy"
          :name="sxzy_xs"
          :flag="math3"
          :table-name="unTableName"
        ></co-level-picker>
      <co-select prop="jkzk" label="健康状况" v-model="data.jkzk"
        :list="codeTable.cdg_healthstate"></co-select>
      <co-input prop="sj" label="手机号码" type="tel"
        v-model="data.sj" placeholder="请输入手机号码"></co-input>
      <co-level-picker label="户籍地行政区划" prop="hjdxzqh" v-model="data.hjdxzqh"
        :name="data.hjdxzqh_xs" table-name="cdg_regioncode" :flag="math1"></co-level-picker>

      <co-level-picker label="居住地行政区划" prop="jzdxzqh" v-model="data.jzdxzqh"
        :name="data.jzdxzqh_xs" :flag="math2" table-name="cdg_regioncode"></co-level-picker>

      <co-input prop="email" label="邮箱（选填）" type="email"
       v-model="data.email" placeholder="请输入邮箱"></co-input>

    </co-form>
    <div class="btn-wrap" v-if="nextFlag">
      <button class="weui-btn weui-btn_primary" @click.prevent="save(0)" type="button">保存</button>
    </div>
    <div class="btn-wrap" v-else>
      <button class="weui-btn weui-btn_primary" @click.prevent="save(1)" type="button">下一步</button>
    </div>
  </div>
</template>
<script>
import coForm from 'base/form/form';
import coInput from 'base/form/input';
import coSelect from 'base/form/select';
import { validator } from 'common/js/valid';
import coLevelPicker from 'base/form/levelPicker';
import dataPicker from 'base/form/dataPicker';

export default {
  name: 'BaseInfo',
  components: { coForm, coInput, coSelect, coLevelPicker, dataPicker },
  data() {
    return {
      math1: '1',
      math2: '2',
      math3: '3',
      disTrue: true,
      nextFlag: true,
      formHeight:"",
      data: {
        grqzid: '',
        sj: '', // 手机号
        gddh: '', // 固定电话
        email: '',
        hjdxzqh: '', // 户籍地行政区划
        whcd: '', // 文化程度
        jzdxzqh: '', // 居住地行政区划
        jkzk: '',  // 健康状况
        hkszd: '',  // 户口所在地
       },
      rules: {
        xm: validator('32,"full","姓名",false'), // 手机号
        sj: validator('11,"sjh","手机号码",false'), // 手机号
        // gddh: validator('32,"full","固定电话",true'), // 固定电话
        email: validator('48,email,"邮箱（选填）",true'), // 邮箱
        hjdxzqh: validator('12,"full","户籍地行政区划（行政区划）",false'),
        whcd: validator('2,"full","文化程度",false'), // 文化程度
        jzdxzqh: validator('12,"full","居住地行政区划（行政区划）",false'),
        jkzk:  validator('1,"full","健康状况",false'),  // 健康状况
        byyxlb:  validator('2,"full","毕业院校类别",true'),  // 毕业院校类别
        sxzy: validator('168,"full","所学专业",true'),
      },
      codeTable: {
        cdg_healthstate: [], // 健康状况
        zyjsww_d_whcd: [], // 文化程度
      },
      sxzy_xs:'',
      sxzy_chang:'',
      unTableName:"",
      sxzy:false
    };
  },
  watch:{
    'data.whcd': function (val) {
      this.$nextTick(_=>{
        if(this.sxzy_chang != val){
          this.sxzy_xs = "";
          this.data.sxzy = "";
        }
      })
      if(!val) return
      this.sxzy = true;
      if (val <= 17) {
        this.unTableName = "zyjs_d_yjs";
      } else if (val == "20") {
        this.unTableName = "zyjs_d_bk";
      } else if (val == "30") {
        this.unTableName = "zyjs_d_zk";
      } else if (val == "40") {
        this.unTableName = "zyjs_d_zz";
      }else {
        this.sxzy = false;
      }
    }
  },
  methods: {
    save(next) {
      this.$refs.form.validate(() => {
        this.POST_save(next);
      });
    },
    /* 获取代码表 */
    GET_codeTable() {
      const arrTableName = Object.keys(this.codeTable);
      this.$http.get('/dmbgl/dmblbCx', {
        params: {
          table_name: arrTableName.join()
        }
      }).then((res) => {
        this.getCodeTabled = true;
        const data = res.returnData.dmblb;
        arrTableName.forEach(item => {
          this.codeTable[item] = data[item];
        });
      });
    },
    /* 保存基本信息修改 */
    POST_save() {
      if(this.data.whcd<=40 && !this.data.sxzy){
        this.$refs.form.errorMsg = '所学专业不能为空！';
        return
      }
      this.$confirm('确定保存？', '提示')
      .then(() => {
      this.$loading = true;
      this.data.ywcz = 2;
      this.$http.post('/grgl/jbxxBc', this.data).then(res => {
        const resData = res.returnData;
        this.$alert(resData.message, () => {
          const code = +resData.executeResult;
          if (code && !this.nextFlag) {
            this.$router.push({name:'UserResumejobIntent'})
          } else if (code){
            this.$router.push({name:"userResume"});
          }
        });
      }).finally(() => {
        this.$loading = false;
      });
      })
      .catch(() => {
        this.$toast('选择了取消');
      });
    },
    /*  查询基本信息 */
    GET_getInfo() {
      this.$loading = true;
      this.$http.get('/grgl/grxqCx').then(res => {
        const resData = res.returnData;
        this.data = resData.jbxx;
        this.nextFlag = resData.sfyqzdj == '0'? false: true;
        this.sxzy_xs = this.data.sxzy_xs;
        this.sxzy_chang = this.data.whcd;
      }).finally(() => {
        this.$loading = false;
      });
    },
    /* 返回基础信息 */
    backBase() {
      this.$router.go(-1);
    }
  },
  created() {
    // 获取代码表
    this.getCodeTabled = false;
    this.GET_codeTable();
    this.GET_getInfo();
    // 隐藏home按钮
    this.$store.commit('SET_VISIBLE_ALL', false);
  }
};
</script>
<style scoped lang="scss">
@import '~common/scss/var.scss';
.wrap {
  background: $color-bg;
  overflow: auto;
  .btn-wrap {
    display: flex;
    .weui-btn {
      margin: 20px auto;
      flex: 0 1 60%;
      margin-top: $margin-base;
    }
  }
}
/deep/ .form .weui-cells{
    margin-top: 110px;
  }
</style>
